package com.kgc.xsProvider.service.ServiceImpl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.xs.pojo.SalesReturn;
import com.kgc.xs.pojo.SalesReturnDetail;
import com.kgc.xsProvider.dao.SalesReturnMapper;
import com.kgc.xsProvider.service.SalesReturnService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class SalesReturnServiceImpl implements SalesReturnService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private SalesReturnMapper salesReturnMapper;


    @Override
    public ResponseMsg selectSalesReturnByPage(Map<String, Object> map) {
        logger.info("SalesReturnServiceImpl selectSalesReturnByPage params {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        PageInfo<SalesReturn> pageInfo = new PageInfo<>();
        pageInfo.setPageNo(map.get("pageNo")==null?1:Integer.parseInt(map.get("pageNo").toString()));
        pageInfo.setPageSize(map.get("pageSize")==null?10:Integer.parseInt(map.get("pageSize").toString()));
        Page page= PageHelper.startPage(pageInfo.getPageNo(),pageInfo.getPageSize());
        List<SalesReturn> list = salesReturnMapper.selectSalesReturnByPage(map);
        pageInfo.setList(list);
        pageInfo.setTotal((int)page.getTotal());
        responseMsg.setData(pageInfo);
        logger.debug("SalesReturnServiceImpl selectSalesReturnByPage result:{}",responseMsg);
        return responseMsg;
    }

    @Override
    @Transactional
    public ResponseMsg insertSalesReturn(Map<String, Object> map) {
        logger.info("SalesReturnServiceImpl insertSalesReturn params {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        ObjectMapper mapper = new ObjectMapper();
        SalesReturn salesReturn =mapper.convertValue(map.get("salesReturn"),SalesReturn.class);
        List<?> rawDetails = (List<?>) map.get("details");
        List<SalesReturnDetail> details = mapper.convertValue(rawDetails,new TypeReference<List<SalesReturnDetail>>(){});
        int result = salesReturnMapper.insertSalesReturn(salesReturn);
        try {
            for (SalesReturnDetail detail : details) {
                salesReturnMapper.insertSalesReturnDetail(detail);
            }

        }catch (Exception e){
            e.printStackTrace();
            responseMsg.setCode("201");
            responseMsg.setMsg("明细数据储存出错");
        }
        if(result>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("SalesReturnServiceImpl insertSalesReturn result:{}",responseMsg);
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg updateSalesReturn(Map<String, Object> map) {
        logger.info("SalesReturnServiceImpl updateSalesReturn params {}",map.toString());
        deleteSalesReturn(map);
        ResponseMsg responseMsg = insertSalesReturn(map);
        logger.debug("SalesReturnServiceImpl updateSalesReturn result:{}",responseMsg);
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg deleteSalesReturn(Map<String, Object> map) {
        logger.info("SalesReturnServiceImpl deleteSalesReturn params {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        int result1=salesReturnMapper.deleteSalesReturn(map.get("docCode").toString());
        int result2=salesReturnMapper.deleteSalesReturnDetail(map.get("docCode").toString());
        if(result1>0&&result2>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("SalesReturnServiceImpl deleteSalesReturn result:{}",responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg selectSalesReturnDetail(Map<String, Object> map) {
        logger.info("SalesReturnServiceImpl selectSalesReturnDetail params {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        List<SalesReturnDetail> details = salesReturnMapper.selectSalesReturnDetail(map);
        responseMsg.setData(details);
        logger.debug("SalesReturnServiceImpl selectSalesReturnDetail result:{}",responseMsg);
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg checkSalesReturn(Map<String, Object> map) {
        logger.info("SalesReturnServiceImpl checkSalesReturn params {}",map);
        ResponseMsg responseMsg = new ResponseMsg();
        int result=salesReturnMapper.checkSalesReturn(map);
        if(result>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("SalesReturnServiceImpl checkSalesReturn result:{}",responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg selectSalesReturnByCode(String docCode) {
        logger.info("SalesReturnServiceImpl selectSalesReturnByCode params {}",docCode);
        ResponseMsg responseMsg = new ResponseMsg();
        SalesReturn salesReturn = salesReturnMapper.selectSalesReturnByCode(docCode);
        responseMsg.setData(salesReturn);
        logger.debug("SalesReturnServiceImpl selectSalesReturnByCode result:{}",responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg selectSalesReturnDetailByCode(String xsReturnCode) {
        logger.info("SalesReturnServiceImpl selectSalesReturnDetailByCode params {}",xsReturnCode);
        ResponseMsg responseMsg = new ResponseMsg();
        List<SalesReturnDetail>  list = salesReturnMapper.selectSalesReturnDetailByCode(xsReturnCode);
        responseMsg.setData(list);
        logger.debug("SalesReturnServiceImpl selectSalesReturnDetailByCode result:{}",responseMsg);
        return responseMsg;
    }
}
